<template>
    <transition name="fade">
        <div class="login"></div>
    </transition>
</template>

<script>
import { Dialog, Toast } from "vant";
import { mapMutations } from "vuex";
export default {
    name: "Login",
    data() {
        return {
            redirect: "/",
        };
    },
    mounted() {
        // this.getLogin("S2mqHmNvWkCCVkM7_BaqQBNoSIxEBB9qiQJrXra_0oM");
        // return;

        if (this.getQueryVariable("code")) {
            Toast.loading({
                message: "加载中...",
                forbidClick: true,
                duration: 0,
            });
            this.getLogin(this.getQueryVariable("code"));
        } else {
            this.login();
        }

        // test_login();

        // this.getInfo(
        //   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC94d3FhcGkuZWRheWhvc3QxODkuaGFuY2hlbmd2aXAuY29tXC9hcGlcL2xvZ2luIiwiaWF0IjoxNjMyOTg0ODMxLCJleHAiOjE5OTI5ODQ4MzEsIm5iZiI6MTYzMjk4NDgzMSwianRpIjoib0Q4cFVKV1E4SE5BRVJnRyIsInN1YiI6MzMsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.P-fn97Kgd4D1IB1CEXGbIeS7RvacJautNM-fDKU9XAU"
        // );
        // this.plan_type()
    },
    methods: {
        ...mapMutations(["SET_TYPE", "SET_INFO", "SET_PLAN_TYPE", "SET_TAB_TYPE"]),
        login() {
            let appid = "wwdc0bc731d475dfb8";
            let redirect_uri = process.env.VUE_APP_URL;
            let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(
                appid
            )}&redirect_uri=${encodeURI(redirect_uri)}&response_type=code&scope=snsapi_base#wechat_redirect`;
            window.location.replace(url);
        },
        getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return false;
        },
        test_login(code) {
            this.$api
                .login({
                    user_id: code,
                })
                .then((res) => {
                    Toast.clear();
                    if (res.status_code == 400) {
                        Dialog({ message: res.message });
                        return;
                    }
                    if (!res.data.userid) {
                        Dialog({ message: res.message });
                        return;
                    } else {
                        localStorage.setItem("_token_", res.data.token);
                        localStorage.setItem("_userInfo_", JSON.stringify(res.data));
                        // this.info =  JSON.stringify(res.data)
                        // return
                        if (localStorage.getItem("_redirect")) {
                            let url = "/leadShare/#" + localStorage.getItem("_redirect");
                            localStorage.removeItem("_redirect");
                            window.location.replace(url);
                        } else {
                            window.location.replace(process.env.VUE_APP_HOME);
                        }
                    }
                })
                .catch((e) => {
                    console.log(e);
                    Toast("接口错误");
                });
        },
        getLogin(code) {
            this.$api
                .login(code)
                .then((res) => {
                    Toast.clear();
                    if (res.status_code == 400) {
                        Dialog({ message: res.message });
                        return;
                    }
                    if (!res.data.userid) {
                        Dialog({ message: res.message });
                        return;
                    } else {
                        localStorage.setItem("_token_", res.data.token);
                        localStorage.setItem("_userInfo_", JSON.stringify(res.data));
                        // this.info =  JSON.stringify(res.data)
                        // return
                        if (localStorage.getItem("_redirect")) {
                            let url = "/leadShare/#" + localStorage.getItem("_redirect");
                            localStorage.removeItem("_redirect");
                            window.location.replace(url);
                        } else {
                            window.location.replace(process.env.VUE_APP_HOME);
                        }
                    }
                })
                .catch((e) => {
                    console.log(e);
                    Toast("接口错误");
                });
        },
    },
    // watch: {
    //   $route: {
    //     handler: function (val) {
    //       this.redirect = val?.query?.redirect;
    //     },
    //     immediate: true,
    //   },
    // },
};
</script>

<style lang="scss" scoped>
.login {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    background-color: white;
}
.fade-enter-active,
.fade-leave-active {
    transition: all 0.3s;
}
.fade-enter,
.fade-leave-to {
    transform: translateX(100%);
}
</style>
